#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Data;
using Flynn.Core.Activities;
using Flynn.Core.ActivityResults;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Core.Tests.Activities
{
    [TestFixture]
    public class ExecuteUpdateActivityFixture
    {
        private MockRepository _mocks;

        private IExecuteSqlStatementsActivity _executeSqlStatementsActivity;
        private IExecuteUpdateActivity _executeUpdateActivity;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _executeSqlStatementsActivity = _mocks.CreateMock<IExecuteSqlStatementsActivity>();
            _executeUpdateActivity = new ExecuteUpdateActivity(_executeSqlStatementsActivity);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ExecuteSqlStatementsActivity_Must_Not_Be_Null()
        {
            new ExecuteUpdateActivity(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Executing_Update_Update_Must_Not_Be_Null()
        {
            _executeUpdateActivity.ExecuteUpdate(null, _mocks.Stub<IDbConnection>());
        }   

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Executing_Connection_Update_Must_Not_Be_Null()
        {
            _executeUpdateActivity.ExecuteUpdate(new Update("1"), null);
        }   

        [Test]
        public void Can_Execute_Update()
        {
            Update update = new Update("1");
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_executeSqlStatementsActivity.ExecuteSqlStatements(update.SqlStatements, connection)).Return(ExecuteSqlStatementsResult.Success());
            }

            using (_mocks.Playback())
            {
                ExecuteUpdateResult result = _executeUpdateActivity.ExecuteUpdate(update, connection);

                Assert.IsFalse(result.Failed);
            }
        }

        [Test]
        public void Executing_Update_Fails_If_Executing_SqlStatements_Fails()
        {
            Update update = new Update("1");
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_executeSqlStatementsActivity.ExecuteSqlStatements(update.SqlStatements, connection)).Return(ExecuteSqlStatementsResult.Failure("Failure message"));
            }

            using (_mocks.Playback())
            {
                ExecuteUpdateResult result = _executeUpdateActivity.ExecuteUpdate(update, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Executing_Update_Fails_If_Executing_SqlStatements_Throws_Exception()
        {
            Update update = new Update("1");
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_executeSqlStatementsActivity.ExecuteSqlStatements(update.SqlStatements, connection)).Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                ExecuteUpdateResult result = _executeUpdateActivity.ExecuteUpdate(update, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }
    }
}